<?php

namespace app\api\controller;

use app\BaseController;
use app\api\model\Singers as ModelSingers;
use app\api\model\Song as ModelSong;
use app\service\TimeService;
use think\Request;

class Song extends BaseController
{
    public function index(Request $request)
    {
        $limit = isset($request['limit']) ? $request['limit'] : 10;
        $keyword = isset($request['keyword']) ? $request['keyword'] : null;
        $singer_id = isset($request['singer_id']) ? $request['singer_id'] : null;

        $query= ModelSong::with('singer')->whereNull('deleted_at');

        if ($keyword) {
            $query->whereLike('song_name', '%' . $keyword . '%');
        }

        if ($singer_id) {
            $query->where('singer_id', '=', $singer_id);
        }

        $query->order('id desc');

        $list=$query-> paginate($limit);

        return JsonData(SUCCESS_CODE, "成功", $list);
    }

    public function read($id)
    {
        $item = ModelSong::with('singer')->whereNull('deleted_at')->find(['id' => $id]);

        return JsonData(SUCCESS_CODE, "成功", $item);
    }
}